<?php
/*
 * generazione codice funzioni
 */
function genCodeFun(&$outMsg) {
	global $objSDAO;
	$outMsg = _t("GENERAZIONE FUNZIONI")."\n";
	$retCode = 0;
	$aPrefVar=array('V'=>'P_','A1'=>'X_','A2'=>'Y_');
	if (!is_dir(SERVER_FS_GENCODE)) {
		return _t("directory %s inesistente",SERVER_FS_GENCODE);
	}
	if (!is_dir(SERVER_FS_GENCODE."/function")) {
		mkdir(SERVER_FS_GENCODE."/function",0775);
	}
	$sSQL = "SELECT f.library, f.function, f.description, f.language, f.result_type, " .
			"    fv.name, f.script, fv.variable_type " .
			" FROM function f " .
			" LEFT JOIN function_variable fv ON fv.function = f.function" .
			" ORDER BY f.library, f.function, fv.sequence";
	$objSDAO->query($sSQL,"fun");
	$row = $objSDAO->fetch("fun");
	while($row) {
		$library = $row['library'];
		$outMsg .= "   "._t("Libreria %s",$library)."\n";
		$funcFile = SERVER_FS_GENCODE."/function/sxLib_$library.inc.php";
		if (!$fh = fopen($funcFile, "wb")) {
			$outMsg .= _t("impossibile creare file %s",$funcFile)."\n";
			return 2;
		}		
		fwrite($fh, "<?php\r\n");
		// genera include delle funzioni scritte in PHP
		fwrite($fh, "if (file_exists(FS_WEB_ROOT.'/com/inc/sxLib/$library.inc.php')) {\r\n");
		fwrite($fh, "   require_once(FS_WEB_ROOT.'/com/inc/sxLib/$library.inc.php');\r\n");
		fwrite($fh, "}\r\n\r\n");
		while($row and $library == $row['library']) {
			$function = $row['function'];
			$language = $row['language'];
			$script = $row['script'];
			$fdes = $row['description'];
			$initSL = "   \$aVar = array();\r\n";
			switch($row['result_type']) {
				case "A1":
					$returnVar = "X_return";
					$initPHP = "   \$X_return = array();\r\n";
					$initSL .= "   \$aVar['X_return'] = array();\r\n";
					break;
				case "A2":
					$returnVar = "Y_return";
					$initPHP = "   \$Y_return = array();\r\n";
					$initSL .= "   \$aVar['Y_return'] = array();\r\n";
					break;
				default:
					$returnVar = "W_return";
					$initPHP = "   \$W_return = '';\r\n";
					$initSL .= "   \$aVar['W_return'] = '';\r\n";
					break;
			}
			$aFuncVar = array();
			$sFuncPar = "";
			$sep = '$';
			while ($row and $function == $row['function']) {
				if (!empty($row['name'])) { 
					$var = $aPrefVar[$row['variable_type']].$row['name'];
					$aFuncVar[] = $var;
					$sFuncPar .=$sep.$var;
					$initSL .= "   \$aVar['$var']=$var;\n";
					$sep = ', $';
				}
				$row = $objSDAO->fetch("fun");
			}
			if(strtoupper($language) == "SL"){
				//parsing dello script in Sophix Language
				$objParse = new sophixParser;
				if(!$objParse->parseFunc($script)){
					$outMsg .= _t("Funzione %s (%s) - errore parsing",$function, $language)."\n";
					$retCode = 1;
					continue;
				} 
				fwrite($fh, "/**\r\n * $function ($language) - $fdes\r\n */\r\n");
				fwrite($fh, "function sxFunc_$function($sFuncPar) {\r\n");
				fwrite($fh, $initSL);
				fwrite($fh, $objParse->getPHPFuncCode($returnVar));
				fwrite($fh, "\r\n\r\n");
			} else {
				// language == "PHP"
				continue ;  
			}
			$outMsg .= "      "._t("Funzione %s (%s) - codice generato correttamente",$function,$language)."\n";
		}
		fwrite($fh, "?>");
		fclose($fh);
		//@chmod($funcFile,0664);
		$outMsg .= "      -- "._t("libreria %s generata correttamente",$library)."\n";
	}
	$outMsg .= _t("-- generazione funzioni terminata --")."\n";
	return $retCode;
}

/*
 * generazione codice script
 */
function genScriptCode($nome,&$objParse) {
	global $objDB;
	$puFile = RGL_FS_CODGEN_DIR.'/funct.'.$nome.".php";
	if (!$fh = fopen($puFile, "wb")) {
		return _t("impossibile creare file %s",$puFile)."\n";
	}		
	fwrite($fh, "<?php\r\n");
	fwrite($fh, "/**\r\n * ".str_repeat('=',50)."\r\n");
	fwrite($fh, " * Script ".$nome."\r\n");
	fwrite($fh, " * ".str_repeat('=',50)."\r\n */\r\n");
	fwrite($fh, $objParse->getPHPCode("rgl_script_".$nome));
	fwrite($fh, "?>\r\n");
	fclose($fh);
	return '';
}

?>
